package com.click369.mianshiqingbao.controller;

import com.click369.mianshiqingbao.pojo.CommentBean;
import com.click369.mianshiqingbao.pojo.UserBean;
import com.click369.mianshiqingbao.pojo.entity.CommentViewBean;
import com.click369.mianshiqingbao.pojo.entity.PageResult;
import com.click369.mianshiqingbao.pojo.entity.Result;
import com.click369.mianshiqingbao.service.CommentService;
import com.click369.mianshiqingbao.service.UserService;
import com.click369.mianshiqingbao.util.StaticFinalFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 题目评论信息的控制器类
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;

    /**
     * 条件分页查询题目评论信息列表
     * @param commentbean 题目评论信息对象
     * @param page 页码
     * @param size 每页记录数
     * @return 分页查询信息的封装对象
     */
    @RequestMapping("/search.do")
    @ResponseBody
    public PageResult search(@RequestBody CommentBean commentbean, int page, int size){
        return commentService.findPage(commentbean,page, size);
    }

    /**
     * 新增题目评论信息
     * @param commentbean 题目评论信息对象
     * @return 成功或失败的提示信息封装类对象
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public Result add(@RequestBody CommentBean commentbean){
        try {
            commentbean.setCommentTime(new Date());
            commentService.add(commentbean);
            return new Result(true, StaticFinalFile.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StaticFinalFile.FAILURE);
        }
    }

    /**
     * 根据id查询题目评论信息对象
     * @param id 题目评论ID
     * @return 题目评论信息对象
     */
    @RequestMapping("/findOne.do")
    @ResponseBody
    public CommentBean findOne(Long id){
        return commentService.findOne(id);
    }

    /**
     * 修改题目评论信息
     * @param commentbean 题目评论信息对象
     * @return 成功或失败的提示信息封装类对象
     */
    @RequestMapping("/update.do")
    @ResponseBody
    public Result update(@RequestBody CommentBean commentbean){
        try {
            commentService.update(commentbean);
            return new Result(true,StaticFinalFile.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StaticFinalFile.FAILURE);
        }
    }

    /**
     * 批量删除题目评论信息
     * @param ids 包含有被删除题目评论信息的ID数组
     * @return 成功或失败的提示信息封装类对象
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Result delete(Long ids[]){
        try {
            commentService.delete(ids);
            return new Result(true,StaticFinalFile.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StaticFinalFile.FAILURE);
        }
    }
    /**
     * 根据当前题目Id获取评论列表
     * @param topicId 题目Id
     * @return 评论列表
     */
    @RequestMapping("/getCommentBeanList.do")
    @ResponseBody
    public PageResult getCommentBeanList(Long topicId){
        List<CommentViewBean> commentViewBeanList = new ArrayList<CommentViewBean>();
        try {
            List<CommentBean> commentBeanList = commentService.getCommentBeanList(topicId);
            if(commentBeanList.size()>0){
                for(CommentBean commentBean:commentBeanList){
                    CommentViewBean commentViewBean = new CommentViewBean();
                    UserBean userBean = userService.findOne(commentBean.getUserId());

                    if (userBean.getUserNick()!=null && userBean.getUserNick().length()>0){
                        commentViewBean.setCommenter(userBean.getUserNick());
                    }else {
                        String phone = userBean.getTelNumber();
                        String newphone = phone.replace(phone.substring(3,7),"****");
                        commentViewBean.setCommenter(newphone);
                    }
                    commentViewBean.setContent(commentBean.getCommentContent());
                    commentViewBeanList.add(commentViewBean);
                }
                return new PageResult(commentViewBeanList.size(),commentViewBeanList);
            }else {
                return new PageResult(0,commentViewBeanList);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(0,commentViewBeanList);
        }
    }
}
